﻿<?php

include('dbConfig.php');

function charsetConv($str) {
	global $configDbEncoding;
	return iconv($configDbEncoding.'//TRANSLIT', 'utf-8', $str);
}

class DbPostgres {
	private $host;
	private $port;
	private $user;
	private $pass;
	private $dbName;
	//private $dbPrefix;
	private $conn;
	
	private $numrows;
	private $result;
	
	public function __construct()
	{
		global $configDbHost;
		global $configDbPort;
		global $configDbUser;
		global $configDbPass;
		global $configDbName;
		//global $configDbPrefix;
	
		$this->host 	= $configDbHost;
		$this->port		= $configDbPort;
		$this->user 	= $configDbUser;
		$this->pass 	= $configDbPass;
		$this->dbName 	= $configDbName;
		//$this->dbPrefix = $configDbPrefix;
		$this->conn		= NULL;
		$this->numRows	= -1;
	}
	
	public function connect()
	{
		if($this->conn == NULL)
			$this->conn = pg_connect('host='.$this->host.' port='.$this->port.' dbname='.$this->dbName.' user='.$this->user.' password='.$this->pass);
		if($this->conn > 0)
			return true;
		else
			return false;	
	}
	
	public function disconnect()
	{
		if($this->conn != NULL)
			if(pg_close($this->conn))
			{
				$this->conn = NULL;
				return true;
			}
			else
				return false;
	}
	
	public function query($query)
	{
		/*if($this->dbPrefix != '') {
		
		}*/
	
		if($this->result = pg_query($this->conn, $query))
			$this->numRows = pg_numrows($this->result);
		else
			$this->numRows = -1;
		return $this->numRows;
	}		
	
	/*private function addPrefix($query) {
	
		return $query;
	}*/
			
	public function numRows()
	{
		return $this->numRows;
	}
	
	public function fetchArray()
	{
		while($row = pg_fetch_array($this->result, null, PGSQL_ASSOC))
			$return[] = $row;
		return $return;
	}
}

?>